//
//  YJHGuidePageViewController.m
//  NaoXinFaWu
//
//  Created by linzhijun on 16/5/25.
//  Copyright © 2016年 naoxin_Ltd. All rights reserved.
//
#import "YJHGuidePageViewController.h"
#import "YJHGuidePageCollectionViewCell.h"

@interface YJHGuidePageViewController ()
/** 记录偏差*/
@property (nonatomic ,assign)CGFloat lastOffset;

@property (nonatomic ,weak)UIImageView *guid;
@property(nonatomic,strong)UIPageControl *pageControl;
@end

@implementation YJHGuidePageViewController
static NSString * const reuseIdentifier = @"Cell";

// 1.设置布局参数

// 2.注册cell

// 3.自定义cell

- (instancetype)init{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置尺寸
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    
//    NSLog(@"%@",NSStringFromCGRect(XMGKeyWindow.bounds));
    
    // 设置行间距
    flowLayout.minimumLineSpacing = 0;
    
    // 设置cell之间的间距
    flowLayout.minimumInteritemSpacing = 0;
    
    // 设置滚动方向 水平
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置每一组的间距
//    flowLayout.sectionInset = UIEdgeInsetsMake(100, 20, 30, 40);
    
    return [super initWithCollectionViewLayout:flowLayout];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //  self.view != self.collectionView
    // self.collectionView添加到 self.view
    
//    self.view.backgroundColor = [UIColor redColor];
//
//    self.collectionView.backgroundColor = [UIColor yellowColor];
    
//    self.collectionView.width = 100;
//    self.collectionView.height = 100;
    
    
    // 1.注册cell
    [self.collectionView registerClass:[YJHGuidePageCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 2.初始化
    [self setup];
    
    // 3.添加子控件
//    [self setupAddChildImageView];
}
// 添加子控件
- (void)setupAddChildImageView {

    _pageControl=[[UIPageControl alloc]init];
    _pageControl.numberOfPages=GuidePageNum;
    _pageControl.currentPageIndicatorTintColor=DMColor(254, 174, 81);
    _pageControl.pageIndicatorTintColor = DMColor(254, 235, 213);
    _pageControl.center=CGPointMake(self.view.nx_width/2, self.view.nx_height*0.9);
    [self.view addSubview:_pageControl];
}
#pragma mark - scorllViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    
}

// 初始化
- (void)setup{
    // 2.禁止弹簧效果
    self.collectionView.bounces = NO;
    // 3.隐藏水平的滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    // 4.设置分页
    self.collectionView.pagingEnabled = YES;
}
#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return GuidePageNum;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
   YJHGuidePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    
    // 更换图片
    NSString *name = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    UIImage *image = [UIImage imageNamed:name];
    
    // 给cell赋值
    cell.image = image;
    
//    NSLog(@"%@",cell);
    
    [cell setIndexPath:indexPath count:GuidePageNum];
  
    return cell;
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger i=scrollView.contentOffset.x/ScreenWidth;
    _pageControl.currentPage=i;
    if (i==(GuidePageNum-1)) {
        _pageControl.hidden=YES;
    }else
    {
        _pageControl.hidden=NO;
    }
    
}

@end
